package com.iu.control;

import com.google.gson.Gson;
import com.iu.bean.NoteBean;
import com.iu.dao.NoteDao;
import com.iu.dao.UserDao;
import com.iu.uitl.Result;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.crypto.Data;
import java.io.IOException;
import java.util.Date;

@WebServlet("/insertNote")
public class InsertNoteServlet extends HttpServlet {
    NoteBean noteBean;
    NoteDao noteDao;
    Result result;

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        noteBean = new NoteBean();
        noteBean.setTitle(request.getParameter("title"));
        noteBean.setContent(request.getParameter("content"));
        noteBean.setCreateTime(new NoteDao().dataToString(new Date()));
        noteBean.setUserId(new UserDao().getIdByTel(request.getParameter("phone")));
        noteBean.setNoteTime(request.getParameter("noteTime"));
        noteDao = new NoteDao();
        result = new Result();
        result.isSuccess = noteDao.insertNote(noteBean);
        result.msg = result.isSuccess ? "插入成功" : "插入失败";
        Gson gson = new Gson();
        String res = gson.toJson(result);
        response.getWriter().append(res);

    }
}
